package org.tobaccomain.service.serviceImpl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.tobaccomain.mapper.DeskMapper;
import org.tobaccomain.mapper.ExtraMapper;
import org.tobaccomain.pojo.dto.PageExtra;
import org.tobaccomain.pojo.dto.TobaccoInfo;
import org.tobaccomain.pojo.entity.Rfid;
import org.tobaccomain.pojo.entity.TobaccoBasic;
import org.tobaccomain.pojo.vo.PageResult;
import org.tobaccomain.service.ExtraService;

import java.io.IOException;
import java.time.Year;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ExtraServiceImpl implements ExtraService {

    @Resource
    private ExtraMapper extraMapper;
    @Resource
    private DeskMapper deskMapper;

    //备料信息添加
    @Override
    @Transactional
    public void addExtra(TobaccoBasic tobaccoBasic) {
        extraMapper.insertExtra(tobaccoBasic);
        //设置rfid不可用
        deskMapper.setRfidUnavailable(tobaccoBasic.getRfid());
        //更新备料信息的个数
        deskMapper.updateExtra(1);
    }

    //删除备料烟信息
    @Override
    @Transactional
    public void delExtra(TobaccoBasic tobaccoBasic) {
        extraMapper.deleteExtra(tobaccoBasic.getId());
        //设置rfid可用
        deskMapper.setRfidAvailable(tobaccoBasic.getRfid());
        //更新备料信息的个数
        deskMapper.updateExtra(-1);
    }

    //备料信息修改
    @Override
    public void modifyExtra(TobaccoBasic tobaccoBasic) {
        extraMapper.updateExtra(tobaccoBasic);
    }

    //备注信息修改
    @Override
    public void modifyExtraInfo(TobaccoInfo extraInfo) {
        extraMapper.updateExtraInfo(extraInfo);
    }

    //备料信息的分页查询
    @Override
    public PageResult getExtraList(PageExtra pageExtra) {
        //查询总记录数
        Integer total = extraMapper.count(pageExtra);
        //查询记录
        List<TobaccoBasic> list = extraMapper.selectExtraList(pageExtra);
        //封装返回的对象
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(total);
        return pageResult;
    }

    //根据id查询备料信息
    @Override
    public TobaccoBasic getExtraById(Integer id) {
        return extraMapper.selectExtraById(id);
    }

    //备料信息导入
    @Override
    @Transactional
    public void uploadExtra(MultipartFile file) throws IOException {
        XSSFWorkbook excel = new XSSFWorkbook(file.getInputStream());
        //读出数据
        XSSFSheet sheet = excel.getSheetAt(0);
        int rowNum = sheet.getLastRowNum();
        //定义列表存储数据
        List<TobaccoBasic> list =new ArrayList<>();
        //定义列表存储rfid号
        Long rfidNum;
        List<Rfid> rfidList = new ArrayList<>();
        for (int i = 1; i <=rowNum ; i++) {
            TobaccoBasic tobacco = new TobaccoBasic();
            XSSFRow row = sheet.getRow(i);
            rfidNum = (long) row.getCell(0).getNumericCellValue();
            tobacco.setRfid(rfidNum);
            //需要把rfid提取出来存入rfid表中，并设置使用状态为0（已使用）
            Rfid rfid = new Rfid();
            rfid.setValue(rfidNum);
            rfid.setTag(0);
            rfidList.add(rfid);
            tobacco.setName(row.getCell(1).getStringCellValue());
            tobacco.setLocation(row.getCell(2).getStringCellValue());
            Year year = Year.of((int) row.getCell(3).getNumericCellValue());
            tobacco.setYear(year);
            tobacco.setPart(row.getCell(4).getStringCellValue());
            tobacco.setEvaluate((int) row.getCell(5).getNumericCellValue());
            XSSFCell cell = row.getCell(6);
            if (cell != null) {
                tobacco.setInfo(cell.getStringCellValue());
            }
            list.add(tobacco);
        }
        //关闭资源
        excel.close();
        //将rfid数据上传至数据库
        deskMapper.saveRfid(rfidList);
        //将单料烟数据入库
        extraMapper.uploadExtra(list);
        //更新单料烟的个数
        deskMapper.updateExtra(list.size());

    }


}
